package nccloud.web.dm.delivbill.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import nc.itf.dm.feecalculate.deliv.ICancelCalculate;
import nc.pubitf.dm.m4804.IDelivBilMaintain;
import nc.vo.dm.m4804.entity.DelivBillAggVO;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pubapp.pattern.model.transfer.bill.ClientBillCombinServer;
import nc.vo.pubapp.pattern.model.transfer.bill.ClientBillToServer;
import nccloud.dto.dm.pub.entity.BatchInfo;
import nccloud.dto.dm.pub.entity.BatchInfos;
import nccloud.dto.scmpub.script.entity.SCMScriptResultDTO;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.core.json.IJson;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.json.JsonFactory;
import nccloud.framework.web.ui.pattern.extbillcard.ExtBillCard;
import nccloud.framework.web.ui.pattern.extbillcard.ExtBillCardOperator;
import nccloud.web.dm.delivbill.utils.DelivbillPrecisionHandler;
import nccloud.web.scmpub.ccarriertransfer.utils.CarrierTransUtils;




public class DelivbillUnApFeeCalculateAction
  implements ICommonAction
{
  public Object doAction(IRequest request) {
    IJson json = JsonFactory.create();
    String read = request.read();
    BatchInfos infos = (BatchInfos)json.fromJson(read, BatchInfos.class);
    DelivBillAggVO[] vos = getVos(infos);
    ClientBillToServer<DelivBillAggVO> clientToServer = new ClientBillToServer<DelivBillAggVO>();
    DelivBillAggVO[] toServerBills = (DelivBillAggVO[])clientToServer.construct(vos);
    DelivBillAggVO[] viewvo = cancelCalculate(toServerBills[0]);
    ClientBillCombinServer<DelivBillAggVO> combinserver = new ClientBillCombinServer<DelivBillAggVO>();
    combinserver.combine(vos, viewvo);
    ExtBillCardOperator operator = new ExtBillCardOperator(infos.getPagecode());
    ExtBillCard billcard = operator.toCard(vos[0]);
    
    CarrierTransUtils.translateForm(billcard.getHead());
    DelivbillPrecisionHandler.getInstance().processCard(billcard);
    SCMScriptResultDTO result = new SCMScriptResultDTO();
    result.setData(billcard);
    return result;
  }





  
  private DelivBillAggVO[] cancelCalculate(DelivBillAggVO toServerBill) {
    DelivBillAggVO[] vos = null;
    ICancelCalculate cal = (ICancelCalculate)ServiceLocator.find(ICancelCalculate.class);
    try {
      DelivBillAggVO retvo = cal.cancelCalculate(toServerBill);
      vos = new DelivBillAggVO[] { retvo };
    } catch (Exception ex) {
      ExceptionUtils.wrapException(ex);
    } 
    return vos;
  }






  
  private DelivBillAggVO[] getVos(BatchInfos infos) {
    List<String> ids = new ArrayList<String>();
    Map<String, UFDateTime> tss = new HashMap<String, UFDateTime>();
    for (BatchInfo info : infos.getInfos()) {
      ids.add(info.getId());
      tss.put(info.getId(), info.getTs());
    } 
    DelivBillAggVO[] vos = null;
    
    try {
      vos = ((IDelivBilMaintain)ServiceLocator.find(IDelivBilMaintain.class)).queryDelivBillByPks((String[])ids.toArray(new String[0]));
    } catch (BusinessException e) {
      ExceptionUtils.wrapException(e);
    } 
    if (vos == null || vos.length < ids.size()) {
      ExceptionUtils.wrapBusinessException(
          NCLangRes4VoTransl.getNCLangRes().getStrByID("4014001_0", "04014001-0401"));
    }
    
    for (DelivBillAggVO vo : vos) {
      vo.getParentVO().setTs((UFDateTime)tss.get(vo.getParentVO().getCdelivbill_hid()));
    }
    return vos;
  }
}
